import axios from "axios";
import {Message} from "element-ui";
import router from '../router'

let baseUrl = "http://localhost:8080/"
// let token = localStorage.getItem("token");

//创建axios的实例
const service = axios.create({
    //接口前缀
    baseURL:baseUrl,
    //设置接口的超时时间
    timeout:360000,
})
//请求的拦截器
service.interceptors.request.use((config)=>{
    config.headers["token"] = localStorage.getItem("token");

    return config;
})
//请求响应拦截器
service.interceptors.response.use((res)=>{
    console.log("响应拦截器:"+res)
    if(res.data.code === 500){
        Message.error(res.data.msg)
        return false
    }else if(res.data.code === 401){
        Message.error(res.data.msg)
        //跳转到登录页
        router.push("/login")
        return false;
    }
    return res.data;
})
export function sendRequest(method,url,data){
    return service({
        method:method,
        url:url,
        data:data
    })
}
export function getBaseUrl(){
    return baseUrl;
}